package com.mrjy.common.qtz;

import com.mrjy.common.entity.RecordIncomeInfo;
import com.mrjy.common.entity.TransferInfo;
import com.mrjy.maintain.bo.RecordIncomeInfoBo;
import com.mrjy.maintain.dao.IRecordIncomeInfoDao;
import com.mrjy.payment.bo.TransferPaymentBo;
import com.mrjy.payment.dao.ITransferInfoDao;
import org.apache.log4j.Logger;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * 语音收入零钱结算
 */
public class RecordSettlementJob {

    public static Integer counter = 0;

    @Resource
    TransferPaymentBo transferPaymentBo;
    @Resource
    IRecordIncomeInfoDao recordIncomeInfoDao;
    @Resource
    ITransferInfoDao transferInfoDao;


    private static Logger logger = Logger.getLogger(RecordSettlementJob.class);

    /**
     * @param
     * @Description: 执行任务，语音收入零钱结算
     */
    public void doJob() {

        synchronized (RecordSettlementJob.counter) {
            logger.debug("语音收入零钱结算");

            //企业付款零钱到推广者微信账户
            List<RecordIncomeInfo> recordIncomeInfos = recordIncomeInfoDao.queryIncomeAll();

            for (RecordIncomeInfo recordIncomeInfo : recordIncomeInfos) {
                TransferInfo transInfo = new TransferInfo();
                transInfo.setAmount(recordIncomeInfo.getMoney());
                transInfo.setPartnerTradeNo("record" + System.currentTimeMillis());
                transInfo.setUserId(recordIncomeInfo.getBeneficiaryId());
                transInfo.setPaymentDesc("语音收入结算成功");
                transInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
                Map<String, Object> returnMap = (Map<String, Object>) transferPaymentBo.weChatTransferPay(transInfo);
                transInfo.setCompleteTime(new Timestamp(System.currentTimeMillis()));
                transInfo.setDisabledFlag(0 + "");
                if (returnMap.get("err_code").equals("200")) {
                    transInfo.setTransferStatus(1 + "");
                    recordIncomeInfo.setSettleStatus(2);//结算成功

                    int update = recordIncomeInfoDao.updateIncomeByUserId(recordIncomeInfo);
                    if (update < 1) {
                        transInfo.setTransferStatus(4 + "");
                        transInfo.setPaymentDesc("语音收入结算失败,紧急处理");
                        logger.fatal("userid:" + recordIncomeInfo.getBeneficiaryId() + " 语音收入结算失败");
                    }
                } else {
                    transInfo.setTransferStatus(3 + "");
                    transInfo.setPaymentDesc("语音收入结算失败");
                    logger.warn("userid:" + recordIncomeInfo.getBeneficiaryId() + " 语音收入结算失败");
                }
                int insert = transferInfoDao.insert(transInfo);
                logger.debug(returnMap);
            }
        }


    }
}